//
//  LGTrainDetailActionView.m
//  XieHeHeartDisease
//
//  Created by 1234 on 2024/2/29.
//

#import "LGTrainDetailActionView.h"

@interface LGTrainDetailActionView()


@end

@implementation LGTrainDetailActionView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithString:@"#F1FAF7"];
        self.cornerRidus = 5;
        self.clipsToBounds = YES;
    }
    return self;
}

-(void)setActionArray:(NSArray *)actionArray{
    _actionArray = actionArray;
    NSInteger count = (isPad == YES)?4:2;
    CGFloat itemW = (isPad == YES)?((Screen_W-viewPix(32))/4.0):((Screen_W-viewPix(32))/2.0);
    CGFloat itemH = viewPix(30);
    for (NSInteger i=0; i<actionArray.count; i++) {
        UIView *itemView = [self createItemView:actionArray[i] index:i];
        itemView.frame = CGRectMake(itemW*(i%count), itemH*(i/count)+viewPix(5), itemW, itemH);
        [self addSubview:itemView];
    }
}

-(NSString *)timeString:(NSString *)seconds{
    NSInteger hour = [seconds integerValue]/3600;
    seconds = [NSString stringWithFormat:@"%ld",[seconds integerValue]-3600*hour];
    NSInteger minute = [seconds integerValue]/60;
    NSInteger second = [seconds integerValue]%60;
    NSString *content = [NSString stringWithFormat:@"%.2ld:%.2ld",minute,second];
    if(hour > 0){
        content = [NSString stringWithFormat:@"%.2ld:%@",hour,content];
    }
    return content;
}

-(UIView *)createItemView:(NSDictionary *)tempDic index:(NSInteger)index{
    UIView *baseView = [[UIView alloc]init];
    UILabel *titleLabel = [UILabel lableWithText:LGNSString(tempDic[@"title"]) colorString:@"#909399" textFont:LGFont(12) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:titleLabel];
    UILabel *valueLabel = [UILabel lableWithText:[self timeString:LGNSString(tempDic[@"train_time"])] colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:valueLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(baseView).offset(viewPix(16));
        make.right.lessThanOrEqualTo(valueLabel.mas_left).offset(-viewPix(5));
        make.centerY.equalTo(baseView);
    }];
    [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(baseView).offset(viewPix(72));
        make.centerY.equalTo(baseView);
    }];
    return baseView;
}
@end
